package com.yuanfengerp.inventorycheck.formcheck.service.impl;

import com.yuanfengerp.inventorycheck.formcheck.dao.ICheckmanagmentDao;
import com.yuanfengerp.inventorycheck.formcheck.model.Checkmanagement;
import com.yuanfengerp.inventorycheck.formcheck.service.ICheckmanagementService;
import com.yuanfengerp.inventorycheck.missioncheck.dao.IChecktaskDao;
import com.yuanfengerp.inventorycheck.missioncheck.model.Checktask;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CheckmanagementImpl implements ICheckmanagementService {

    @Autowired
    ICheckmanagmentDao ckmt;

    @Autowired
    IChecktaskDao ckt;
    @Override
    public Result<List<Checkmanagement>> selectCheckmanagementAll(Map<String, Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Checkmanagement> list = ckmt.selectCheckmanagementAll(params);
        if (!(list.isEmpty())){
            return Result.success(list, ckmt.Count());
        }
        return Result.failure(400,"没有数据");
    }

    @Override
    public Result<String> addCheckmanagement(Checkmanagement checkmanagement) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        checkmanagement.setDocument_ID(idWorker.nextId());
        Checktask select = ckt.select(checkmanagement.getRelevance_ID());
        checkmanagement.setCheck_state(select.getCheck_state());
        checkmanagement.setCheck_category(select.getCheck_type());
        checkmanagement.setWarehouse_name(select.getWarehouse_name());
        checkmanagement.setWarehouse_ID(select.getWarehouse_ID());
        checkmanagement.setAudit_state(0);
        checkmanagement.setOperation_time(new Date(System.currentTimeMillis()));
        checkmanagement.setAudit_time(new Date(System.currentTimeMillis()));
        Integer integer = ckmt.addCheckmanagement(checkmanagement);
        if (integer > 0){
            return Result.success("添加成功");
        }
        return Result.failure(400,"添加失败");
    }

    @Override
    public Result<Checkmanagement> select(String document_ID) {
        Checkmanagement select = ckmt.select(document_ID);
        if (select != null){
            return Result.success(select);
        }
        return null;
    }


}
